﻿//Select的处理
function ControlSelect(obj) {
    //获取Select项的个数
    this.GetOptionCount = function () {
        return $(obj).get(0).options.length;
    }

    //获得选中项的索引
    this.GetSelectIndex = function () {
        return $(obj).get(0).selectedIndex;
    }

    //获得当前选中项的文本
    this.GetSelectText = function () {
        if (this.GetOptionCount() == 0) {
            alert("Options is null!");
            return "Options is null";
        }
        else {
            var index = this.GetSelectIndex();
            return $(obj).get(0).options[index].text;
        }
    }

    //获得当前选中项的值
    this.GetSelectValue = function () {
        if (this.GetOptionCount() == 0) {
            alert("Options is null!");
            return "Options is null";
        } else {
            return $(obj).val();
        }
    }

    //设置select中值为value的项为选中
    this.SetSelectByValue = function (value) {
        $(obj).get(0).value = value;
    }

    //设置select中文本为text的第一项被选中
    this.SetSelectByText = function (text) {
        var isExist = false;
        var count = this.GetOptionCount();
        for (var i = 0; i < count; i++) {
            if ($(obj).get(0).options[i].text == text) {
                this.SetSelectIndex(i);
                isExist = true;
                break;
            }
        }
        if (!isExist) {
            alert("该项不存在!");
        }
    }

    //设置选中指定索引项
    this.SetSelectIndex = function (index) {
        var count = this.GetOptionCount();
        if (index >= count || index < 0) {
            alert("超出索引!");
            return;
        }
        else {
            $(obj).get(0).selectedIndex = index;
        }
    }

    //判断select项中是否存在值为value的项
    this.isExistItemByValue = function (value) {
        var isExist = false;
        var count = this.GetOptionCount();
        for (var i = 0; i < count; i++) {
            if ($(obj).get(0).options[i].value == value) {
                isExist = true;
                break;
            }
        }
        return isExist;
    }

    //判断select项中是否存在文本为text的项
    this.isExistItemByText = function (text) {
        var isExist = false;
        var count = this.GetOptionCount();
        for (var i = 0; i < count; i++) {
            if ($(obj).get(0).options[i].text == text) {
                isExist = true;
                break;
            }
        }
        return isExist;
    }

    //向select中添加一项，显示内容为text，值为value,如果该项值已存在，则提示
    this.AddOption = function (text, value) {
        var isExist1 = this.isExistItemByValue(value);
        var isExist2 = this.isExistItemByText(text);
        if (isExist1) {
            alert("该value已存在!");
            return;
        }
        if (isExist2) {
            alert("该text已存在!");
            return;
        }
        else {
            $(obj).get(0).options.add(new Option(text, value));
        }
    }

    //删除select中值为value的项，如果该项不存在，则提示
    this.DeleteOptionByValue = function (value) {
        var isExist = this.isExistItemByValue(value);
        if (isExist == false) {
            alert("该项不存在!");
            return;
        } else {
            var count = this.GetOptionCount();
            for (var i = 0; i < count; i++) {
                if ($(obj).get(0).options[i].value == value) {
                    $(obj).get(0).remove(i);
                    break;
                }
            }
        }
    }

    //删除select中文本为text的项，如果该项不存在，则提示
    this.DeleteOptionByText = function (text) {
        var isExist = this.isExistItemByText(text);
        if (isExist == false) {
            alert("该项不存在!");
            return;
        } else {
            var count = this.GetOptionCount();
            for (var i = 0; i < count; i++) {
                if ($(obj).get(0).options[i].text == text) {
                    $(obj).get(0).remove(i);
                    break;
                }
            }
        }
    }

    //删除select中指定索引的项
    this.DeleteOptionByIndex = function (index) {
        var count = this.GetOptionCount();
        if (index >= count || index < 0) {
            alert("超出索引!");
            return;
        }
        else {
            $(obj).get(0).remove(index);
        }
    }

    //删除select中选定的项
    this.DeleteSelectedOption = function () {
        var index = this.GetSelectIndex();
        $(obj).get(0).remove(index);
    }

    //清除select中的所有项
    this.ClearOptions = function () {
        $(obj).get(0).options.length = 0; 
    }
}